
<head>
	<meta charset='utf-8' />
	<title>后台管理登陆</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
</head>
<body style='background:#eaa'>
	<h3 style='margin-top:250px;text-align:center' id='Notice'>页面加载中........</h3>
</body>
<script>
	var domain_expire = 3000; 	//域名缓存过期时间


	//控制台打印内容
	function C(r){
		console.log(r);
	}
	



	
	//获取负载服域名列表
	function GetServerDomainList(){
		if(localStorage.DomainList){
		
			var current_time = new Date().getTime();
			if(localStorage.expire && current_time-domain_expire < localStorage.expire){
				C('DomainList_sess');
				return localStorage.DomainList;
			}else{
				C('DomainList_cache1');
				return GetServerDomainList2();//过期了
			}
		}else{
			C('DomainList_cache2');	//全新获取
			return GetServerDomainList2();
		}
		function GetServerDomainList2(){
			var domain_url = 'DomainList/domain_list.html?t='+Math.random();
			var res = AjaxSyncJs(domain_url);
			if(/^\["http.*\]$/.test(res)){
				localStorage.DomainList = res;
			}else{
				localStorage.DomainList = '';
			}
			C(localStorage.DomainList);
			localStorage.expire = new Date().getTime();
			return localStorage.DomainList;
		}
	}

	//GET同步请求同域文件
	function AjaxSyncJs(url){
		if(window.XMLHttpRequest){
		  var ajax=new XMLHttpRequest();
		}else{
		  var ajax=new ActiveXObject("Microsoft.XMLHTTP");
		}
		ajax.open('get',url,0);  
		ajax.send();
		return ajax.responseText;
	}
	


	
	
	
	
	
	/*
		### url列表中随机一个并判断该页面是否可用
		参一：array   域名列表
		参二：fun     回调函数，回参返回一个可用页面url
		参三：string  localStore使用的缓存名，默认为 'tmp_domain_list'
		参四：int     url不可用的超时最大时间，默认为 500 毫秒
		场外参数： fail_count int  必须  用来计算尝试远程页面失败次数
		说明：
			会将列表中的url以随机抽取并判断是否可用，可用则返回，
			不可用则再次随机抽取，直到获取可用url并返回
		示例： 
			var arr = [ "http://store9.shop.top/ping.html", "http://store8.shop.top/ping.html", "http://store7.shop.top/ping.html"];

			var fail_count = 0;
			CheckAndSelectUrl(arr,function(url){ 
				if(url){
				
				}else{
					C(8888888);
				}

			},'abcdefg',500);
	*/
	var fail_count = 0;
	function CheckAndSelectUrl(domains_arr,fun,sess_name,timeout){
		var timeout = timeout?timeout:500;
		var sess_name = sess_name?sess_name:'tmp_domain_list';
		var store_str = 'localStorage.'+sess_name;
		var store_str_save = store_str+"='"+JSON.stringify(domains_arr)+"'";
		if(!eval(store_str)){
			eval(store_str_save);
		}	
		var domains = JSON.parse(eval(store_str));
		//C(domains);
		var rand = Math.floor(Math.random()*domains.length);
		var current_path = domains.splice(rand,1)[0];
		if(domains.length > 0){
			//每次抽完就减少一个，以达到全部轮询
			var store_str_save2 = store_str+"='"+JSON.stringify(domains)+"'";
			eval(store_str_save2);
		}else{
			//抽光后，重置域名数组
			eval(store_str_save);
		}
		UrlIsActive(current_path,function(s){
			//C(fail_count);
			if(!s){
				if(fail_count >= domains_arr.length * 2){
					fun(false);
				}else{
					fail_count ++;
					CheckAndSelectUrl(domains_arr,fun);
				}
			}else{
				fail_count = 0;
				fun(current_path);
			}
		},timeout);
	}
	
	
	

	
	/*
		### 判断远程url是否可用
		参一：请求的url
		参二：回调
		参三：超时，默认1000毫秒
		示例：
			UrlIsActive(url,function(s){
				C(s);
			});
	*/
	function UrlIsActive(url,fun,timeout){
		if(!timeout){
			var timeout = 1000;
		}
		var ping_stat = false;
		if(window.XMLHttpRequest){
		  var ajax=new XMLHttpRequest();
		}else{
		  var ajax=new ActiveXObject("Microsoft.XMLHTTP");
		}
		ajax.open('GET',url,1);
		ajax.send();
		ajax.onreadystatechange = function(){
			if(ajax.readyState == 4 && ajax.status == 200){
				ping_stat = true;
				fun(ping_stat);
			}
		}
		setTimeout(function(){
			if(!ping_stat){
				fun(ping_stat);
			}
		},timeout);
	}
	
	
	


	var DomainListStr = GetServerDomainList();	//一：获取负载服域名列表
	C(DomainListStr);
	if(/^\[.+\]$/.test(DomainListStr)){
		var domains = JSON.parse(DomainListStr);	
		var domains2 = [];
		for(var i=0;i<domains.length;i++){
			var tmp = decodeURIComponent(domains[i]) + '/ping.html';//拼接检测页面地址,并合成数组
			domains2.push(tmp);
		}
		C(domains2);
		CheckAndSelectUrl(domains2,function(url){ 
			if(url){ 		//获取到有效远程域名地址
				var path = url.substring(0,url.lastIndexOf('/'))+'/zadmin/login.html';
				C(path);
				location.href=path;
			}else{
				SendNotice('域名列表中无可用域名');
			}
		});
	}else{
		SendNotice('获取域名列表异常');
	}

	function SendNotice(info){
		C(info);
		Notice.innerHTML = '系统维护中，请稍候再试！';
		//需要向管理员发送警告
	}

	



	

	
	
	
	
</script>